 /* Copyright (c) 2013 fab-o-lab

 Permission is hereby granted, free of charge, to any person
 obtaining a copy of this software and associated documentation
 files (the "Software"), to deal in the Software without
 restriction, including without limitation the rights to use,
 copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the
 Software is furnished to do so, subject to the following
 conditions:

 The above copyright notice and this permission notice shall be
 included in all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
*/


package com.fabolab.jetset.core;

import java.io.File;
import java.io.IOException;

import android.content.SharedPreferences;

import com.fabolab.jetset.core.servlet.AndroidContextServlet;
import com.fabolab.jetset.core.servlet.ServletException;
import com.fabolab.jetset.core.servlet.ServletOutputStream;
import com.fabolab.jetset.core.servlet.http.HttpServletRequest;
import com.fabolab.jetset.core.servlet.http.HttpServletResponse;
import com.fabolab.jetset.core.template.MiniTemplator;


public class SecureAdminServlet extends AndroidContextServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 96784321257336453L;
	/**
	 * 
	 */

	public static final String PREFS_NAME = "JetSetPrefs";

	// / Returns a string containing information about the author, version, and
	// copyright of the servlet.
	public String getServletInfo() {
		return "admin servlet";
	}

	// / Services a single request from the client.
	// @param req the servlet request
	// @param req the servlet response
	// @exception ServletException when an exception has occurred
	public void service(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		
		log("sadmin called");
		MiniTemplator template = new MiniTemplator(context.getDocumentPath()
				+ File.separator + "admin/admin.html");
		if(req.getUserPrincipal() != null){
			template.setVariableOptEsc("user", req.getUserPrincipal().getName());
			if(req.isUserInRole("user")){
				log("user role ok!");
			}
			if(req.isUserInRole("admin")){
				log("admin role ok!");
			}
		if (req.getMethod().equalsIgnoreCase("post")) {
			log("admin post");
			String port = (String) req.getParameter("port");
			
			log("admin port: " + port);
			// All objects are from android.context.Context
			SharedPreferences settings = service.getSharedPreferences(
					PREFS_NAME, 0);
			SharedPreferences.Editor editor = settings.edit();
			editor.putInt("port", Integer.valueOf(port));

			// Commit the edits!
			editor.commit();
			template.setVariableOptEsc("port", String.valueOf(settings.getInt("port", 9090)));

			log("sadmin post end");
		} else {
			log("sadmin get");
			SharedPreferences settings = service.getSharedPreferences(
					PREFS_NAME, 0);
			template.setVariableOptEsc("port",
					String.valueOf(settings.getInt("port", 9090)));
			log("sadmin get end");

		}
		}
		if(req.getUserPrincipal() != null){
			res.setStatus(HttpServletResponse.SC_OK);
		}else{
			res.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
		}
		//WWW-Authenticate: Basic realm="insert realm"
		res.setHeader("WWW-Authenticate","Basic realm=\"JetSetRealm\"");
		res.setContentType("text/html");

			ServletOutputStream p = res.getOutputStream();
			if(req.getUserPrincipal() != null){
				p.write(template.generateOutput().getBytes());
			}
			p.flush();
			p.close();

	}



}
